@extends('layouts.admin-iframe-base')


@section('css')

@stop

@section('content')
        <!-- Content Header (Page header) -->
<section class="content-header">
    <h1>
        用户管理
        <small>所有用户</small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
        <li class="active">首页</li>
    </ol>
</section>

<!-- Main content -->
<section class="content">

    <!-- Your Page Content Here -->
    <div class="box">
        <div class="box-header">
            <h3 class="box-title">用户列表</h3>
        </div>
        <!-- /.box-header -->
        <div class="box-body">
            <table id="J-dt" class="table table-bordered table-hover">
                <thead>
                <tr>
                    <th>用户名</th>
                    <th>邮箱</th>
                    <th>手机号</th>
                    <th>添加时间</th>
                    <th>电站</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>
        <!-- /.box-body -->
    </div>
    <!-- /.box -->

</section>
<!-- /.content -->


@stop

@section('js')
    <script src="/dist/admin/plugins/datatables/jquery.dataTables.min.js"></script>
    <script src="/dist/admin/plugins/datatables/dataTables.bootstrap.min.js"></script>
    <script>

        $(function () {
            $('#J-dt').DataTable({
                "paging": true,
                "lengthChange": false,
                "searching": true,
                "ordering": true,
                "info": true,
                "autoWidth": false,
                "ajax": "/admin/user/search",
                "columns": [
                    {"data": "name"},
                    {"data": "email"},
                    {"data": "phone"},
                    {"data": "created_at"},
                    {"data": "id"},
                    {"data": "id"}
                ],
                "columnDefs": [
                    {
                        "targets": -1,
                        "render": function (data, type, row) {
                            var str = '<a href="/admin/user/edit-user?id=' + data + '" class="btn btn-success btn-sm">编辑</a> &nbsp;';
                            str = str + '<a href="javascript:;" class="btn btn-danger btn-sm J-del-btn" data-id="' + data + '">删除</a>';
                            return str;
                        }
                    },
                    {
                        "targets": -2,
                        "render": function (data, type, row) {
                            return '<a href="/admin/site/user-site?user_id=' + data + '" class="btn btn-info btn-sm" >他的发电站</a>';
                        }
                    }

                ]
            });
            $('#J-dt').on('click', '.J-del-btn', function () {
                var id = $(this).data('id');
                $.post('/admin/user/delete-user', {id: id}, function (res) {
                    if (res.status == 1) {
                        alert('删除成功！');
                        window.location.reload();
                    } else {
                        alert(res.error_msg);
                    }
                }, 'json');
            });
        });


    </script>
@stop